package com.spooky.se.parser;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import com.spooky.se.config.Config;

public class ServiceReader {
	private List<File> mServices = null;

	public ServiceReader() {
		mServices = new LinkedList<File>();
	}

	public List<File> read() {
		File root = new File(Config.SERVICE_ROOT);
		// TODO log reading root
		read(root);
		return mServices;
	}

	private void read(File file) {
		if (file.isFile()) {
			visit(file);
		} else if (file.isDirectory()) {
			for (File entity : file.listFiles()) {
				read(entity);
			}
		}
	}

	private void visit(File file) {
		if (isService(file)) {
			mServices.add(file);
		}
	}

	private boolean isService(File file) {
		// TODO EXPAND WITH MORE CONTROLL
		// TODO EXPAND WITH MORE SUPPORT FOR GETTING FILE TYPE
		return getFileExtension(file).equalsIgnoreCase("xml");
	}

	private String getFileExtension(File file) {
		int pos = file.getName().lastIndexOf('.');
		if (pos != -1) {
			return file.getName().substring(pos);
		}
		return "";
	}
}
